import { Context } from 'koa'
import jwt from 'jsonwebtoken'
import { secret_key } from '../config/secret'

export const success = (
  ctx: Context,
  data: Recordable<any> = {},
  msg = 'success',
  code = 200
): { code: number; msg: string; data: Recordable<any> } => {
  ctx.status = 200
  return { code, msg, data }
}

export const handleCreateToken = async (ctx: Context, username: string) => {
  // 生成token
  const token = await jwt.sign({ username }, secret_key, { expiresIn: 60 * 60 })
  //   console.warn('----- my data is token: ', token)
  ctx.cookies.set('token', token, {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    maxAge: 3600 * 1000
  })
}
